package com.cardbaobao.cardbabyclient.ui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.cardbaobao.cardbabyclient.R;
import com.cardbaobao.cardbabyclient.base.Constant;
import com.cardbaobao.cardbabyclient.model.Banner;
import com.cardbaobao.cardbabyclient.model.UserBindCards;
import com.cardbaobao.cardbabyclient.ui.adapter.base.CommonRecyclerAdapter;
import com.cardbaobao.cardbabyclient.ui.adapter.base.CommonRecyclerHolder;
import com.cardbaobao.cardbabyclient.ui.helper.BankCardBgHelper;
import com.cardbaobao.cardbabyclient.utils.DensityUtil;
import com.cardbaobao.cardbabyclient.utils.ImageLoaderUtil;
import com.cardbaobao.cardbabyclient.utils.ScreenUtil;
import com.cardbaobao.cardbabyclient.utils.URLUtil;

import java.text.DecimalFormat;
import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by FHW
 * on 2017/11/7
 */

public class CreditRepaymentAdapter extends CommonRecyclerAdapter<CreditRepaymentAdapter.RepaymentObject> {
    public static final int TYPE_ADD_CARD = 0;
    public static final int TYPE_AD = 1;
    public static final int TYPE_ITEM = 2;

    private RepaymentItemClicked listener;

    private DecimalFormat df = new DecimalFormat("#0.00");

    public void setRepaymentItemClicked(RepaymentItemClicked listener) {
        this.listener = listener;
    }

    public interface RepaymentItemClicked {
        int POSITION_ADD_CARD = 1;//添加信用卡
        int POSITION_ITEM_AD = 2;//点击广告
        int POSITION_UNBIND_CARD = 3;//解绑信用卡
        int POSITION_REPAYMENT = 4;//还款

        void onItemPositionClicked(CreditRepaymentAdapter.RepaymentObject object, int position);
    }

    public CreditRepaymentAdapter(Context context, List<RepaymentObject> data) {
        super(context, data, R.layout.item_credit_card_repayment_new);
    }

    @Override
    public void convert(CommonRecyclerHolder holder, RepaymentObject repaymentObject) {
        UserBindCards.CreditCard creditCard = (UserBindCards.CreditCard) repaymentObject.getObject();

        holder.itemView.setBackgroundResource(BankCardBgHelper.getBankCardBgRes(creditCard.getBack_color()));
        ImageLoaderUtil.displayImage(URLUtil.CBB_IMG_URL + creditCard.getPicture(), (CircleImageView) holder.getView(R.id.civ_logo));
        holder.setTextViewText(R.id.tv_bank, creditCard.getCard_type_name());

        String name = creditCard.getUser_name();
        holder.setTextViewText(R.id.tv_name, name);
//        StringBuilder nameSb = new StringBuilder();
//        int nameLength = name.length();
//        if (nameLength == 2) {
//            holder.setTextViewText(R.id.tv_name, name.substring(0, 1) + " * ");
//        } else {
//            for (int i = 0, length = name.length(); i < length; i++) {
//                if (i == 0 || i == length - 1) {
//                    nameSb.append(name.substring(i, i + 1));
//                } else {
//                    nameSb.append(" * ");
//                }
//            }
//            holder.setTextViewText(R.id.tv_name, nameSb.toString());
//        }

        String cardNumber = creditCard.getCredit_card_no();
        StringBuilder sb = new StringBuilder(Constant.CARD_PREFIX);
        int length = cardNumber.length();
        for (int i = length - 4; i < length; i++) {
            sb.append(cardNumber.charAt(i));
        }
        holder.setTextViewText(R.id.tv_card, sb.toString());
        if (!creditCard.getLatest_repayment_amount().equals("")) {
            holder.setTextViewText(R.id.tv_repayment_money, df.format(Double.parseDouble(creditCard.getLatest_repayment_amount())));
        }

        long time = System.currentTimeMillis() - creditCard.getModify_time().getTime();

        if (time > Constant.MILLISECOND_24_HOUR) {//超过24小时
            holder.getView(R.id.tv_repayment).setVisibility(View.VISIBLE);
            holder.getView(R.id.rl_bot).setVisibility(View.GONE);
        } else {//24小时内
            holder.getView(R.id.tv_repayment).setVisibility(View.GONE);
            holder.getView(R.id.rl_bot).setVisibility(View.VISIBLE);
            holder.getView(R.id.ll_money).setVisibility(View.GONE);
            switch (creditCard.getLatest_repayment_status()) {
                case "1":
//                    holder.setTextViewText(R.id.tv_state, "还款成功");
                    holder.setTextViewText(R.id.tv_state, "已到账");
                    holder.getView(R.id.ll_money).setVisibility(View.VISIBLE);
                    break;
                case ""://为空表示没有还过款
                    holder.getView(R.id.tv_repayment).setVisibility(View.VISIBLE);
                    holder.getView(R.id.rl_bot).setVisibility(View.GONE);
                    break;
                case "2":
                    holder.setTextViewText(R.id.tv_state, "还款失败");
                    break;
                case "3":
//                    holder.setTextViewText(R.id.tv_state, "还款中");
                    holder.setTextViewText(R.id.tv_state, "2小时内到账");
                    holder.getView(R.id.ll_money).setVisibility(View.VISIBLE);
                    break;
//                case "4":
//                    holder.setTextViewText(R.id.tv_state, "退款成功");
//                    break;
//                case "5":
//                    holder.setTextViewText(R.id.tv_state, "退款失败");
//                    break;
//                case "6":
//                    holder.setTextViewText(R.id.tv_state, "退款中");
//                    break;
            }

        }

    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        int viewType = getItemViewType(position);
        final int realPosition = holder.getAdapterPosition();
        if (viewType == TYPE_ADD_CARD) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (listener != null) {
                        listener.onItemPositionClicked(null, RepaymentItemClicked.POSITION_ADD_CARD);
                    }
                }
            });
        } else if (viewType == TYPE_AD) {
            final RepaymentObject object = mData.get(realPosition);
            Banner.CarouselFigureListBean bean =
                    (Banner.CarouselFigureListBean) object.getObject();
            ImageLoaderUtil.displayImage(URLUtil.CBB_IMG_URL + bean.getImage(),
                    (ImageView) holder.itemView);
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (listener != null) {
                        listener.onItemPositionClicked(object, RepaymentItemClicked.POSITION_ITEM_AD);
                    }
                }
            });
        } else {
            if (viewType == TYPE_ITEM) {
                CommonRecyclerHolder itemHolder = (CommonRecyclerHolder) holder;
//                itemHolder.getView(R.id.tv_repayment).setOnClickListener(new View.OnClickListener() {
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (listener != null) {
                            listener.onItemPositionClicked(mData.get(realPosition), RepaymentItemClicked.POSITION_REPAYMENT);
                        }
                    }
                });
                convert(itemHolder, mData.get(position));
            }
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        if (viewType == TYPE_ADD_CARD) {
            //添加信用卡
            View root = inflater.inflate(R.layout.item_credit_dash_add, parent, false);
            ((TextView) root.findViewById(R.id.tv_add_card)).setText("添加信用卡");
            return new RecyclerView.ViewHolder(root) {
            };
        }
        if (viewType == TYPE_AD) {
            //广告图宽高比686:210
            ImageView imageView = new ImageView(parent.getContext());
            int margin = DensityUtil.dp2px(16);
            int screenWidth = ScreenUtil.getScreenWidth() - margin * 2;
            RecyclerView.MarginLayoutParams layoutParams =
                    new RecyclerView.MarginLayoutParams(screenWidth,
                            (int) (screenWidth * 210 / 686f));
            layoutParams.setMargins(margin, 0, margin, 0);
            imageView.setLayoutParams(layoutParams);
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            return new RecyclerView.ViewHolder(imageView) {
            };
        }
        if (viewType == TYPE_ITEM) {
            View root = inflater.inflate(R.layout.item_credit_card_repayment_new, parent, false);
            return new CommonRecyclerHolder(root);
        }
        return null;
    }

    @Override
    public int getItemViewType(int position) {
        CreditRepaymentAdapter.RepaymentObject item = mData.get(position);
        return item.getType();
    }

    public static class RepaymentObject {
        int type;
        Object object;

        public RepaymentObject() {
        }

        public RepaymentObject(int type, Object object) {
            this.type = type;
            this.object = object;
        }

        public int getType() {
            return type;
        }

        public void setType(int type) {
            this.type = type;
        }

        public Object getObject() {
            return this.object;
        }

        public void setObject(Object object) {
            this.object = object;
        }
    }


}
